package cn.antia.cwin;

import java.util.Map;
import java.util.HashMap;

/**
 * User: Xiao Jacob
 * Date: 2008-7-13
 * Time: 17:09:33
 */
public class HtmlWidget {
    private String htmlType; //HTML的控件类型
	private String dispText; //标签文字
	private Map<String,String> options; //显示列表，假如在HTML控件中显示为SELECT或radio，则需要此项 =
    private Map<String,String> attrs;       //HTML自写选项，包括类似于 cols=10 rows=20 class=input_1 size=10,onClick="" 之类
    private static String[] intAttrs = new String[]{"cols","rows","size"};
    
    private Integer index; //输出顺序
	private String limits;	//验证字符串
    private String comment;			//注释，有关于这个字段的注释(验证出错的提示) =
    
    public String getHtmlType() {
        return htmlType;
    }

    public void setHtmlType(String htmlType) {
        this.htmlType = htmlType;
    }

    public String getDispText() {
        return dispText;
    }

    public Map<String, String> getAttrs() {
        return attrs;
    }
    public void setAttrs(Map<String, String> attrs) {
        this.attrs = attrs;
    }

    public void setAttr(String attr, String value) {
        if (attrs == null) {
            attrs = new HashMap<String, String>();
        }
        attrs.put(attr, value);
    }

    public String getAttr(String attr) {
        if (attrs == null || attrs.size() < 1) {
            return null;
        }
        return attrs.get(attr);
    }

    public void setDispText(String dispText) {
        this.dispText = dispText;
    }

    public Map<String,String> getOptions() {
        return options;
    }

    public void setOptions(Map<String,String> options) {
        this.options = options;
    }
    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public String getLimits() {
        return limits;
    }

    public void setLimits(String limits) {
        this.limits = limits;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    /**
     * 取自定义属性的串
     * @return 属性串
     */
    public String getAttrsString() {
        StringBuilder sb = new StringBuilder();
        if (attrs != null && attrs.size() > 0) {
            boolean hasMsg = false;
            boolean hasLimit = false;
            for (Map.Entry<String, String> entry : attrs.entrySet()) {
                String key = entry.getKey();
                if (!hasMsg) {
                    if (key.equals("msg")) {
                        hasMsg = true;
                    }
                }
                if (!hasLimit) {
                    if (key.equals("limit")) {
                        hasLimit = true;
                    }
                }
                sb.append(entry.getKey()).append("=").append(getAttrValue(entry.getKey(),entry.getValue())).append(" ");
            }
            if (!hasMsg && comment != null && comment.trim().length() > 0) {
                sb.append("msg=\"").append(comment).append("\"");
            }
            if (!hasLimit) {
                sb.append("limit=\"").append(limits).append("\"");
            }
        }
        return sb.toString();
    }


    private String getAttrValue(String attr,String value) {
        for (String at : intAttrs) {
            if (at.equalsIgnoreCase(attr)) {
                return value;
            }
        }
        return new StringBuilder("\"").append(value).append("\"").toString();
    }
}
